package com.parenting.controller;


import com.parenting.entities.SystemResourcesDO;
import com.parenting.enums.ResultEnum;
import com.parenting.exception.BaseException;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.ResourcesTreeRequest;
import com.parenting.response.MenuTreeListResponse;
import com.parenting.service.SystemResourcesService;
import com.parenting.service.SystemUserService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-19
 */
@RestController
@Api(tags = "资源管理", description = "账号授权、授权信息查询、新增授权、修改授权")
@RequestMapping("/resources")
@Slf4j
public class SystemResourcesController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemResourcesService systemResourcesService;

    /***
     * 保存 用户与 按钮资源的关系
     * @param userId
     * @param resourcesIds
     */
    /***
     * 保存 用户与 按钮资源的关系
     * @param userId
     * @param resourcesIds
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true),
            @ApiImplicitParam(name = "resourcesIds", value = "资源Id集合", required = true,
                    allowMultiple = true, dataType = "String"),
    })
    @ApiOperation("新增店员资源关系数据接口")
    //@CheckParam("user:authorize")
    @PostMapping("save/res/{userId}")
    public Result   saveUserAndResources(@PathVariable("userId") String userId,
                                         @RequestBody List<String> resourcesIds){
        log.info("======================saveUserAndResources==============================");
        if (null==resourcesIds || resourcesIds.size()<=0){
            throw new BaseException(ResultEnum.ACCOUNT_JURISDICTION.getCode(),ResultEnum.ACCOUNT_JURISDICTION.getMessage());
        }else{
            systemUserService.saveUserAndResources(userId,resourcesIds);
            return  Result.buildSuccess();
        }

    }

    @ApiOperation("获得所有的权限资源,type 只查询菜单，传入1 or 2 均可，只查询按钮传3 查询所有 传null")
    @PostMapping("Tree")
    public Result<List<SystemResourcesDO>>  queryTreeResources(@RequestBody ResourcesTreeRequest type){

        return   systemResourcesService.queryTreeResources(type);

    }

    @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    @ApiOperation("获得指定用户所有的权限资源，用于用户授权")
    @PostMapping("find/{userId}")
    public Result<List<SystemResourcesDO>> findResourcesByUserId(@PathVariable("userId") String userId){
        List<SystemResourcesDO> byUserId = systemResourcesService.findByUserId(userId);
        return Result.buildSuccess(byUserId);

    }


    @ApiOperation("获得当前登录用户的所有权限资源，用于查询当前登录用户的菜单和按钮权限")
    @PostMapping("find/user/resources")
    public Result<MenuTreeListResponse> findMenuListByUserId(){
        MenuTreeListResponse menuListByUserId = systemResourcesService.findMenuListByUserId(AuthUtil.getUserInfo().getId());
        return Result.buildSuccess(menuListByUserId);

    }


}

